package com.threeman.mapswaypoints1;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class CompassActivity extends Activity implements SensorEventListener{

	private Context mContext;
	private TextView tv1;
	private CompassView mCompassView;
	
	private SensorManager mSensorManager;
	
	float[] inR = new float[16];
	float[] outR = new float[16];
	float[] I = new float[16];
	float[] gravity = new float[3];
	float[] geomag = new float[3];
	float[] orientVals = new float[3];
	
	double azimuth = 0;
	double pitch = 0;
	double roll = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compass);
		mContext = getApplicationContext();
		tv1 = (TextView) findViewById(R.id.textView1Angle);
		mCompassView = (CompassView)this.findViewById(R.id.compassView);
		
		updateOrientation(new float[] {0, 0, 0});
	}
	
    private void updateOrientation(float[] values) {
    	showToastMessage("update");
    if (mCompassView!= null) {
    	mCompassView.setBearing(values[0]);
    	mCompassView.setPitch(values[1]);
    	mCompassView.setRoll(values[2]);
    	mCompassView.invalidate();
    	tv1.setText("Z : " + values[0] + "\nX : " + values[1] + "\nY : " + values[2]);
 	  }
 	}
   	
   	@Override
   	protected void onResume() {
   	  super.onResume();
   	  
   	  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
   	  
   	  //listener dla akcelerometru
   	  mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
   	  //listener dla magnetomentru
   	  mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
   	  
   	}

   	@Override
   	protected void onStop() {
   	  mSensorManager.unregisterListener(this);
   	  super.onStop();
   	}
   	
	private void showToastMessage(String msg)
	{

	      Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);

	      toast.show();

	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	public void onSensorChanged(SensorEvent sensorEvent) {
		
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        	gravity = sensorEvent.values;
        if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    	  geomag = sensorEvent.values;
		
		  float[] values = new float[3];
		  float[] R = new float[9];
		  float[] outR = new float[9];
		  
		
		  SensorManager.getRotationMatrix(R, I, gravity, geomag);
		  SensorManager.remapCoordinateSystem(R, 
		                                      SensorManager.AXIS_Y, 
		                                      SensorManager.AXIS_MINUS_X, 
		                                      outR);
		
		  SensorManager.getOrientation(outR, values);
		
		  // Convert from Radians to Degrees.
		  values[0] = (float) Math.toDegrees(values[0]);
		  values[1] = (float) Math.toDegrees(values[1]);
		  values[2] = (float) Math.toDegrees(values[2]);
		  updateOrientation(values);
		  /*
		// If the sensor data is unreliable return
	    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
	        return;

	    // Gets the value of the sensor that has been changed
	    switch (sensorEvent.sensor.getType()) {  
	        case Sensor.TYPE_ACCELEROMETER:
	            gravity = sensorEvent.values;
	            break;
	        case Sensor.TYPE_MAGNETIC_FIELD:
	            geomag = sensorEvent.values;
	            break;
	    }

	    // If gravity and geomag have values then find rotation matrix
	    if (gravity != null && geomag != null) {

	        // checks that the rotation matrix is found
	        SensorManager.getRotationMatrix(inR, I, gravity, geomag);
	        SensorManager.remapCoordinateSystem(inR, 
                    SensorManager.AXIS_X, 
                    SensorManager.AXIS_Z, 
                    outR);
	        
            SensorManager.getOrientation(outR, orientVals);
            azimuth = Math.toDegrees(orientVals[0]);
            pitch = Math.toDegrees(orientVals[1]);
            roll = Math.toDegrees(orientVals[2]);
            
            updateOrientation(new float[] {(float)azimuth,(float)pitch,(float)roll});
	    }
	    */
	}
}
/*
 * 
 float[] aValues = new float[3];
	float[] mValues = new float[3];
	CompassView compassView;
	SensorManager sensorManager;
	
	TextView tv1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compass);
		mContext = getApplicationContext();
		tv1 = (TextView) findViewById(R.id.textView1Angle);

	  compassView = (CompassView)this.findViewById(R.id.compassView);
	  sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
	  updateOrientation(new float[] {0, 0, 0});
	}
    
    private void updateOrientation(float[] values) {
      if (compassView!= null) {
        compassView.setBearing(values[0]);
        compassView.setPitch(values[1]);
   	    compassView.setRoll(-values[2]);
   	    compassView.invalidate();
   	  }
   	}
    
    private float[] calculateOrientation() {
      float[] values = new float[3];
      float[] R = new float[9];
      float[] outR = new float[9];
      float[] I = new float[9];

      SensorManager.getRotationMatrix(R, I, aValues, mValues);
      SensorManager.remapCoordinateSystem(R, 
                                          SensorManager.AXIS_X, 
                                          SensorManager.AXIS_Z, 
                                          outR);

      SensorManager.getOrientation(outR, values);

      // Convert from Radians to Degrees.
      values[0] = (float) Math.toDegrees(values[0]);
      values[1] = (float) Math.toDegrees(values[1]);
      values[2] = (float) Math.toDegrees(values[2]);
      
      tv1.setText("Z : " + values[0] + "\nX : " + values[1] + "\nY : " + values[2]);

      return values;
    }
    
    private final SensorEventListener sensorEventListener = new SensorEventListener() {
      public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
          aValues = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
          mValues = event.values;

        updateOrientation(calculateOrientation());
      }

      public void onAccuracyChanged(Sensor sensor, int accuracy) {}
   	};
   	
   	@Override
   	protected void onResume() {
   	  super.onResume();

   	  Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   	  Sensor magField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

   	  sensorManager.registerListener(sensorEventListener, 
   	                                 accelerometer, 
   	                                 SensorManager.SENSOR_DELAY_GAME);
   	  sensorManager.registerListener(sensorEventListener, 
   	                                 magField,
   	                                 SensorManager.SENSOR_DELAY_GAME);
   	}

   	@Override
   	protected void onStop() {
   	  sensorManager.unregisterListener(sensorEventListener);
   	  super.onStop();
   	}
 * 
 * */
